{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "a12c8bf3",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests\n",
    "\n",
    "api_key = \"5E5V4cXjdk_y_Fnoa2gIo6Rn2qj5NYjz\"\n",
    "api_secret = \"YlXvJybg0abyNg5bBfISXxjgg417dfhZ\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4dacc6d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 112,\n",
       " 'error_message': 'FACESET_EXIST',\n",
       " 'request_id': '1648974853,b24eb310-a8d3-4590-b7ce-986c830fe801'}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 创建人脸集合\n",
    "create_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"display_name\": \"西1宿舍\",\n",
    "    \"outer_id\": \"4人\",\n",
    "}\n",
    "r = requests.post(create_url, params=payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "24550b12",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 97,\n",
       " 'facesets': [{'faceset_token': '15718ac2976e262e8365a8f5d5ff6c40',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '4799d0fe9d1e2889a64a54c3cff5e5fe',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': 'f791928188afa64ad29dfa6d7e5317fb',\n",
       "   'outer_id': 'API021214',\n",
       "   'display_name': '20级网新API周一晚班',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '4af17d0269fe46ea4dc1f953a4d29f86',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '56ded9f7506d5a6e5bbc665c521b4d06',\n",
       "   'outer_id': '',\n",
       "   'display_name': '',\n",
       "   'tags': ''},\n",
       "  {'faceset_token': '6b1de8a42555f4cf3adf365495a02297',\n",
       "   'outer_id': '4人',\n",
       "   'display_name': '西1宿舍',\n",
       "   'tags': ''}],\n",
       " 'request_id': '1648974854,87645b19-cf66-4907-bc27-dd5250c0c8e4'}"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取人脸库列表及信息\n",
    "getFaces_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getfacesets\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "}\n",
    "\n",
    "r = requests.post(getFaces_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "370d812b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'6b1de8a42555f4cf3adf365495a02297'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 获取 faceset_token\n",
    "faceset_token = r.json()[\"facesets\"][-1][\"faceset_token\"]\n",
    "faceset_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "465f6f92",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b1de8a42555f4cf3adf365495a02297',\n",
       " 'tags': '',\n",
       " 'time_used': 79,\n",
       " 'user_data': '',\n",
       " 'display_name': '西1宿舍',\n",
       " 'face_tokens': [],\n",
       " 'face_count': 0,\n",
       " 'request_id': '1648974907,2d305eb8-2daf-42b4-a60a-bdda36b05387',\n",
       " 'outer_id': '4人'}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 检查数据集合\n",
    "getDetail_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/getdetail\"\n",
    "\n",
    "payload = {\n",
    "    \"api_key\": api_key,\n",
    "    \"api_secret\": api_secret,\n",
    "    \"faceset_token\": faceset_token,\n",
    "}\n",
    "\n",
    "r = requests.post(getDetail_url, payload)\n",
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "f1024121",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 人脸检测，人脸添加，人脸分析，人脸对比，人脸搜索\n",
    "import detect, addFace, compare, analyze, search"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "1fa38bdd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ee17d0cbb2431ad2a28707a31621713c'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第一张图片 token\n",
    "first = detect.face_detect(api_key,api_secret,\"1.jpg\")\n",
    "face01_token = first[\"faces\"][0][\"face_token\"]\n",
    "face01_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "01994081",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b1de8a42555f4cf3adf365495a02297',\n",
       " 'time_used': 521,\n",
       " 'face_count': 1,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648975391,5f08040e-bc04-4433-859d-6d314eb8ab76',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addFace.addFace(api_key, api_secret,faceset_token, face01_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "676aee47",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0f55b20ab98bba40d3b52ff8423e6b13'"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第二张图片 token\n",
    "second = detect.face_detect(api_key,api_secret,\"2.jpg\")\n",
    "face02_token = second[\"faces\"][0][\"face_token\"]\n",
    "face02_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "3b29503c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b1de8a42555f4cf3adf365495a02297',\n",
       " 'time_used': 463,\n",
       " 'face_count': 2,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648975426,5644439b-07f8-4e69-8c2c-0c471aebb4f1',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addFace.addFace(api_key, api_secret,faceset_token, face02_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "7d017963",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ac91f70df3981e62fe1505b3a83b3547'"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第三张图片 token\n",
    "third = detect.face_detect(api_key,api_secret,\"3.jpg\")\n",
    "face03_token = third[\"faces\"][0][\"face_token\"]\n",
    "face03_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "412f0bd7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b1de8a42555f4cf3adf365495a02297',\n",
       " 'time_used': 484,\n",
       " 'face_count': 3,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648975458,873603cc-25c2-47d7-ab04-1757724ef181',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addFace.addFace(api_key, api_secret,faceset_token, face03_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "31eaee98",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2cf0614d021bfb119b200c203e4b88c0'"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 第四张图片 token\n",
    "fourth = detect.face_detect(api_key,api_secret,\"4.jpg\")\n",
    "face04_token = fourth[\"faces\"][0][\"face_token\"]\n",
    "face04_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "0f84db95",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': '6b1de8a42555f4cf3adf365495a02297',\n",
       " 'time_used': 538,\n",
       " 'face_count': 4,\n",
       " 'face_added': 1,\n",
       " 'request_id': '1648975517,cbd7f260-eb9c-4404-9a61-4e7e02ac7ae5',\n",
       " 'outer_id': '4人',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 将人脸添加到集合\n",
    "addFace.addFace(api_key, api_secret,faceset_token, face04_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "36c9cd16",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'743193b0b3827da7c0d1983e68507276'"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 人脸搜索\n",
    "# 准备一张新的照片\n",
    "fifth = detect.face_detect(api_key,api_secret,\"5.jpg\")\n",
    "face05_token = fifth[\"faces\"][0][\"face_token\"]\n",
    "face05_token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "68925288",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648975658,4c47cedd-6ab9-4ff1-8c03-70dc7963df73',\n",
       " 'time_used': 370,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-5': 73.975, '1e-4': 69.101},\n",
       " 'results': [{'confidence': 82.335,\n",
       "   'user_id': '',\n",
       "   'face_token': 'ac91f70df3981e62fe1505b3a83b3547'}]}"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 搜索\n",
    "search.face_search(api_key, api_secret, face05_token, faceset_token)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "40f9ce3f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648975685,541666a8-0f71-4821-87b0-51612c25b5a7',\n",
       " 'time_used': 309,\n",
       " 'confidence': 56.995,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 然后进行对比\n",
    "compareFace = compare.face_compare(api_key, api_secret,face01_token,face05_token)\n",
    "compareFace"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "f2b4ceb0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不是本宿舍成员\n"
     ]
    }
   ],
   "source": [
    "# 最后判断是否为本宿舍成员\n",
    "if compareFace['confidence'] >= compareFace['thresholds']['1e-5']:\n",
    "    print(\"是本宿舍成员\")\n",
    "else:\n",
    "    print(\"不是本宿舍成员\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "3819e484",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 160,\n",
       " 'request_id': '1648975752,7437311c-e92b-4eca-9d15-d8ee427659ac',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 0.027,\n",
       "     'neutral': 0.0,\n",
       "     'disgust': 0.003,\n",
       "     'anger': 0.0,\n",
       "     'surprise': 0.001,\n",
       "     'fear': 0.0,\n",
       "     'happiness': 99.969},\n",
       "    'beauty': {'female_score': 77.795, 'male_score': 71.973},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 20},\n",
       "    'mouthstatus': {'close': 98.07,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.0,\n",
       "     'other_occlusion': 1.93},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 9.357,\n",
       "     'stain': 19.168,\n",
       "     'acne': 2.482,\n",
       "     'health': 26.116},\n",
       "    'headpose': {'yaw_angle': -17.169052,\n",
       "     'pitch_angle': 12.624991,\n",
       "     'roll_angle': -10.883315},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.257},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.257},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.257}},\n",
       "    'smile': {'threshold': 50.0, 'value': 100.0},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.787,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.011,\n",
       "      'no_glass_eye_open': 99.18,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.022},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.191,\n",
       "      'no_glass_eye_close': 0.019,\n",
       "      'occlusion': 0.056,\n",
       "      'no_glass_eye_open': 99.727,\n",
       "      'normal_glass_eye_close': 0.002,\n",
       "      'dark_glasses': 0.004}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 30.186},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.534,\n",
       "      'vector_z_component': 0.991,\n",
       "      'vector_x_component': -0.1,\n",
       "      'vector_y_component': -0.084,\n",
       "      'position_y_coordinate': 0.331},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.59,\n",
       "      'vector_z_component': 0.936,\n",
       "      'vector_x_component': 0.293,\n",
       "      'vector_y_component': 0.194,\n",
       "      'position_y_coordinate': 0.424}}},\n",
       "   'face_rectangle': {'width': 247, 'top': 204, 'left': 215, 'height': 247},\n",
       "   'face_token': '743193b0b3827da7c0d1983e68507276'}]}"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 也可以对图片进行一下分析\n",
    "analyze.face_analyze(api_key, api_secret, face05_token, \"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "60f415cb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
